//
//  HNSH_CartShopCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "HNSH_CartShopCell.h"
#import "HNSH_CartShopCountView.h"

@interface HNSH_CartShopCell ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;

/* selectButton */
@property (nonatomic, strong) UIButton *selectButton;
/* goodsImgV */
@property (nonatomic, strong) UIImageView *goodsImgV;
/* goodsNameLabel */
@property (nonatomic, strong) UILabel *goodsNameLabel;
/* goodsSpecLabel */
@property (nonatomic, strong) UILabel *goodsSpecLabel;
/* goodsPriceLabel */
@property (nonatomic, strong) UILabel *goodsPriceLabel;
/* goodsSendLabel */
@property (nonatomic, strong) UILabel *goodsSendLabel;

/* countView */
@property (nonatomic, strong) HNSH_CartShopCountView *countView;

@end

@implementation HNSH_CartShopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor appBGColor];
        [self configView];
    }
    return self;
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.countView.hidden = isEdit;
}

- (void)setModel:(HNSH_CartShopItemModel *)model {
    _model = model;
    [self.goodsImgV sd_setImageWithURL:[NSURL URLWithString:model.goods_cover_url]];
    self.goodsNameLabel.text = model.goods_name;
    self.goodsSpecLabel.text = model.spec_key_name;
    self.goodsPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.price];
    
    self.countView.count = model.quantity;
    
//    self.goodsSendLabel.hidden = [model.reward_amount doubleValue] <= 0;
    self.goodsSendLabel.text = [NSString stringWithFormat:@"原价：%@",model.reward_amount];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.goodsSendLabel.text attributes:attribtDic];
    // 赋值
    self.goodsSendLabel.attributedText = attribtStr;
//    self.goodsSendLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:self.goodsSendLabel.text SelectContent:@"赠送购物值：" CommonFont:self.goodsSendLabel.font SpeFont:self.goodsSendLabel.font Color:[UIColor appGrayLabelColor]];
    self.selectButton.selected = model.selected;
}

- (void)setShopModel:(HNSH_CartShopModel *)shopModel {
    _shopModel = shopModel;
}

- (void)configView {
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.selectButton];
    [self.bgView addSubview:self.goodsImgV];
    [self.bgView addSubview:self.goodsNameLabel];
    [self.bgView addSubview:self.goodsSpecLabel];
    [self.bgView addSubview:self.goodsPriceLabel];
    [self.bgView addSubview:self.goodsSendLabel];
    [self.bgView addSubview:self.countView];
    
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(40);
    }];
    [self.goodsImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView.mas_centerY);
        make.left.mas_equalTo(40);
        make.width.height.mas_equalTo(88);
    }];
    
    [self.goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsImgV.mas_top);
        make.left.equalTo(self.goodsImgV.mas_right).offset(12);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(kScreenWidth-160);
    }];
    [self.goodsSpecLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsNameLabel.mas_left);
        make.top.equalTo(self.goodsNameLabel.mas_bottom).offset(2);
        make.height.mas_equalTo(17);
    }];
    [self.goodsPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsSpecLabel.mas_left);
        make.bottom.equalTo(self.goodsImgV.mas_bottom);
        make.height.mas_equalTo(22);
    }];
    [self.goodsSendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsPriceLabel.mas_right).offset(4);
        make.bottom.equalTo(self.goodsImgV.mas_bottom);
        make.height.mas_equalTo(17);
    }];
    
}

#pragma mark lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _bgView;
}

- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectButton setImage:[UIImage imageNamed:@"base_select_off"] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamed:@"base_select_on"] forState:UIControlStateSelected];
        WeakSelf(weakSelf);
        [[_selectButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *button = x;
            button.selected = !button.selected;
            weakSelf.model.selected = button.isSelected;
            [weakSelf.shopModel cellChangeSelectedStatus];
            if (weakSelf.reloadBlock) {
                weakSelf.reloadBlock(weakSelf.model);
            }
        }];
    }
    return _selectButton;
}

- (UIImageView *)goodsImgV {
    if (!_goodsImgV) {
        _goodsImgV = [[UIImageView alloc]init];
        _goodsImgV.layer.cornerRadius = 8.0f;
        _goodsImgV.layer.masksToBounds = YES;
    }
    return _goodsImgV;
}

- (UILabel *)goodsNameLabel {
    if (!_goodsNameLabel) {
        _goodsNameLabel = [[UILabel alloc]init];
        _goodsNameLabel.text = @"";
        _goodsNameLabel.textColor = [UIColor appBlackLabelColor];
        _goodsNameLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _goodsNameLabel;
}

- (UILabel *)goodsSpecLabel {
    if (!_goodsSpecLabel) {
        _goodsSpecLabel = [[UILabel alloc]init];
        _goodsSpecLabel.text = @"";
        _goodsSpecLabel.textColor = [UIColor appGrayLabelColor];
        _goodsSpecLabel.font = [UIFont systemFontOfSize:12];
    }
    return _goodsSpecLabel;
}

- (UILabel *)goodsPriceLabel {
    if (!_goodsPriceLabel) {
        _goodsPriceLabel = [[UILabel alloc]init];
        _goodsPriceLabel.text = @"";
        _goodsPriceLabel.textColor = [UIColor appOrangeLabelColor];
        _goodsPriceLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _goodsPriceLabel;
}

- (UILabel *)goodsSendLabel {
    if (!_goodsSendLabel) {
        _goodsSendLabel = [[UILabel alloc]init];
        _goodsSendLabel.text = @"";
        _goodsSendLabel.textColor = [UIColor appGrayLabelColor];
        _goodsSendLabel.font = [UIFont systemFontOfSize:10];
    }
    return _goodsSendLabel;
}

- (HNSH_CartShopCountView *)countView {
    if (!_countView) {
        _countView = [[HNSH_CartShopCountView alloc]initWithFrame:CGRectMake(kScreenWidth-48-63, 85, 63, 18)];
        WeakSelf(weakSelf);
        _countView.countBlock = ^(NSInteger count) {
            weakSelf.model.quantity = count;
            if (weakSelf.reloadBlock) {
                weakSelf.reloadBlock(weakSelf.model);
            }
        };
    }
    return _countView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
